// router.js
import {RouterMount,createRouter} from 'uni-simple-router';
import { getAccessToken } from '@/utils/auth'
import store from '@/store'

const router = createRouter({
	platform: process.env.VUE_APP_PLATFORM,  
	routes: [...ROUTES]
});

// 登录页面
const loginPage = "/pages/login"

// 页面白名单
const whiteList = [
  '/pages/login', '/pages/common/webview/index'
]

const dictList = [
	'/pages/work/device/index'
]

// 检查地址白名单
function checkWhite(url) {
  return whiteList.indexOf(url) !== -1
}

//检查是否需要获取数据字典
function checkDict(url) {
	return dictList.indexOf(url) !== -1
}

//全局路由前置守卫
router.beforeEach((to, from, next) => {
	if (getAccessToken()) {
	  next();
	} else {
	  if (checkWhite(to.path)) {
	    next();
	  }else {
			next(loginPage)
		}
	}
	
});
// 全局路由后置守卫
router.afterEach((to, from) => {
	// 获取数据字典
	if(checkDict(to.path)) {
		store.dispatch('dict/loadDictDatas')
	}
})

export {
	router,
	RouterMount
}